package com.tanhua.admin.controller;

import com.tanhua.admin.service.ManageService;
import com.tanhua.model.db.Freeze;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-31 04:14
 **/
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService manageService;

    /*
    * 查询手机端的注册用户
    *    GET  /users
    * 参数(Query) ：page,pagesize
    *
    *   返回值：
    *       pageResult -- List<UserInfo>
    * */
    @GetMapping("/users")
    public ResponseEntity users(@RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pagesize){
        //调用service
        PageResult vo =  manageService.users(page,pagesize);
        return ResponseEntity.ok(vo);
    }




    /*
    *   查询用户基本资料
    *       GET   /users/:userID
    *       参数： 用户id
           返回值：
            userIdfo
     */

        @GetMapping("/users/{userId}")
        public ResponseEntity findById(@PathVariable("userId") Long userId){
            UserInfo userInfo =manageService.findUserById(userId);
            return ResponseEntity.ok(userInfo);
        }


        /*
        *   根据用户id分页查询用户发布的视频列表
        *       GET  /videos
        *   参数：
        *       page,pagesize,uid(用户id)
        *   返回值：
        *       PageResult  -- list<VideoVo>
        * */
        @GetMapping("videos")
        public ResponseEntity videos(@RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10") Integer pagesize,
                                     Long uid){
            PageResult vo = manageService.findVideoByUserId(uid,page,pagesize);
            return ResponseEntity.ok(vo);
        }

        /*查询动态列表：
        *
        *   GET /messages
        * 参数：
        *    Query pagesize,page, uid(用户id),state(状态)
        *       uid 或者state有可能为空
        * 返回值： pageResult -- List<MovementVo>
        * */

        @GetMapping("/messages")
    public ResponseEntity messages(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   Long uid ,String state){
            /*
            * 前端传递的参数，当空时，前端传递了一个''
            *
            * */
            Integer status = null;
            if (!StringUtils.isEmpty(state) && !"''".equals(state)){
                status = Integer.valueOf(state);
            }
             PageResult vo = manageService.findMovement(uid,status,page,pagesize);
            return ResponseEntity.ok(vo);
        }

        /*
        * 查看动态详情：
        *   GET  /messages/{id}
        *   参数： 消息id
        * 返回值： MovementsVo
        *
        * */
        @GetMapping("/messages/{id}")
        public ResponseEntity findMovements(@PathVariable("id") String id){
            //1.调用service
           MovementsVo vo =  manageService.findMonements(id);
            //2.返回
            return ResponseEntity.ok(vo);
        }

        /*
        * 查看动态评论列表 翻页 后台
        *       GET /messages/comments
        *  参数 ：
        *        pagesize ，page, messageID
        * 返回值：
        *       PageResult --- List<Commentvo>
        * */
        @GetMapping("/messages/comments")
        public ResponseEntity findByComments(@RequestParam(defaultValue = "1") Integer page,
                                             @RequestParam(defaultValue = "10") Integer pagesize,
                                             String messageID){
            PageResult pageResult =  manageService.findByComments(page,pagesize,messageID);
            return ResponseEntity.ok(pageResult);
        }


    /**
     * 动态通过
     * POST /manage/messages/pass
     * Body:    string []	动态id列表
     * 返回数据:    message	操作结果
     */
    @PostMapping("/messages/pass")
    public ResponseEntity pass(@RequestBody String[] ids) {
        Map reMap = manageService.passAndReject(ids,1);
        return ResponseEntity.ok(reMap);
    }

    /**
     * 动态拒绝
     * POST /manage/messages/reject
     * Body:    string []	动态id列表
     * 返回数据:    message	操作结果
     */
    @PostMapping("/messages/reject")
    public ResponseEntity reject(@RequestBody String[] ids) {
        Map reMap = manageService.passAndReject(ids,2);
        return ResponseEntity.ok(reMap);
    }

    /*冻结用户
     * 接口路径 POST  /users/freeze
     * 参数  userId  freezingTime freezingRange  reasonsForFreezing  frozenRemarks
     * 返回值 message */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Freeze freeze){
        // Long userId = freeze.getUserId();

        /*String freezingTime = freeze.getFreezingTime();
        String freezingRange = freeze.getFreezingRange();
        String reasonsForFreezing = freeze.getReasonsForFreezing();
        String frozenRemarks = freeze.getFrozenRemarks();*/

        manageService.userFreeze(freeze);
        String message="操作成功";
        return ResponseEntity.ok(message);
    }
    /*解冻
    接口路径 POST /manage/users/unfreeze
    参数 userId   rereasonsForThawing(解冻原因)
    返回值 message
    * */

    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze( @RequestBody Map map ){
        String userId = map.get("userId").toString();

        manageService.unfreeze(userId);
        String message="操作成功";
        return ResponseEntity.ok(message);
    }


}
